package cn.hxzy.zyicon.controller;

import cn.hxzy.zyicon.entity.Icon;
import cn.hxzy.zyicon.entity.IconGroup;
import cn.hxzy.zyicon.entity.Image;
import cn.hxzy.zyicon.entity.po.IconPO;
import cn.hxzy.zyicon.entity.vo.PageBean;
import cn.hxzy.zyicon.entity.vo.ReturnBean;
import cn.hxzy.zyicon.service.IconService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("icon")
public class IconController {
    @Autowired
    private IconService iconService;

    @PostMapping("list")
    public ReturnBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageBean<Icon> all = iconService.findAll(new PageBean<Icon>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    @PostMapping("searchName")
    public ReturnBean searchName(String searchName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageBean<Icon> all = iconService.findIconByNameLike(searchName, new PageBean<Icon>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    @PostMapping("deleteId")
    public ReturnBean deleteId(Integer id) {
        iconService.delete(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    @PostMapping("add")
    public ReturnBean add(MultipartFile file,Integer groupId,String name) throws IOException {
        Icon icon = new Icon();
        if (file != null && !file.isEmpty()) {
            icon.setImgByte(file.getBytes());
            IconGroup iconGroup = new IconGroup();
            iconGroup.setId(groupId);
            icon.setName(name);
            icon.setOrders(1);
            icon.setGroup(iconGroup);
            iconService.save(icon);
        }
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("update")
    public ReturnBean update(String name, Integer orders, Integer groupId, MultipartFile file, IconPO iconPo, Integer id) throws IOException {
        System.out.println(groupId);
        Icon icon = new Icon();
        IconGroup iconGroup = new IconGroup();
        iconGroup.setId(groupId);
        iconPo.setId(id);
        iconPo.setName(name);
        iconPo.setOrders(orders);
        iconPo.setGroup(iconGroup);
        iconPo.setImgByte(file.getBytes());
        BeanUtils.copyProperties(iconPo, icon);
        iconService.update(icon);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Icon byId = iconService.findById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS,byId);
    }

}
